/*  Copyright (C) 2008  Alexander Blyzniuchenko (BlezAlex)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using Aga.Controls.Tree.NodeControls;
using Aga.Controls.Tree;

namespace MediaDC
{
    class ProgressData
    {
        public int Position;
        public string Text;
    }

    class NodeProgressBar : BindableControl
    {
        private int _value = 50;
        private int _minValue;
        private int _maxValue = 100;
        private string _text;
        private Color _activeFirstColor = Color.FromArgb(0, 49, 43);
        private Color _activeSecondColor = Color.MediumAquamarine;
        private Color _nonActiveFirstColor = Color.FromArgb(39, 39, 39);
        private Color _nonActiveSecondColor = Color.Gray;
        private Color _borderColor = Color.Black;
        private Font _font;
        private Color _foreColor = Color.White;

        #region public properties


        public NodeProgressBar()
        {
            _font = new Font("Arial", 10);
        }

        [DefaultValue(0)]
        [Category("Appearance")]
        [Description("The minimum value.")]
        public int MinValue
        {
            get { return _minValue; }
            set
            {
                _minValue = value;
            }
        }

        [DefaultValue(100)]
        [Category("Appearance")]
        [Description("The maximum value.")]
        public int MaxValue
        {
            get { return _maxValue; }
            set
            {
                _maxValue = value;
            }
        }

        [Category("Appearance")]
        public Color ActiveFirstColor
        {
            get { return _activeFirstColor; }
            set
            {
                _activeFirstColor = value;
            }
        }

        [Category("Appearance")]
        public Color ActiveSecondColor
        {
            get { return _activeSecondColor; }
            set
            {
                _activeSecondColor = value;
            }
        }

        [Category("Appearance")]
        public Color NonActiveFirstColor
        {
            get { return _nonActiveFirstColor; }
            set
            {
                _nonActiveFirstColor = value;
            }   
        }

        [Category("Appearance")]
        public Color NonActiveSecondColor
        {
            get { return _nonActiveSecondColor; }
            set
            {
                _nonActiveSecondColor = value;
            }
        }

        [Category("Appearance")]
        public Color BorderColor
        {
            get { return _borderColor; }
            set
            {
                _borderColor = value;
            }
        }

        [Category("Appearance")]
        public Font Font
        {
            get { return _font; }
            set { _font = value; }
        }

        [Category("Appearance")]
        public Color FontColor
        {
            get { return _foreColor; }
            set { _foreColor = value;}
        }

        #endregion

        public override Size MeasureSize(TreeNodeAdv node, DrawContext context)
        {
            Update(node);
            SizeF s = context.Graphics.MeasureString(_text, _font);
            return new Size((int)s.Width, (int)s.Height);
        }

        public override void Draw(TreeNodeAdv node, DrawContext context)
        {
            Update(node);

            Rectangle r = context.Bounds;
            r.Y += 1;
            if (_value != 0)
                r.Width = (int)(r.Width * (float)_value / (MaxValue - MinValue));
            else
                r.Width = 0;

            DrawGradient(context.Graphics, r, _activeFirstColor, _activeSecondColor);
            r.X += r.Width;
            r.Width = context.Bounds.Width - r.Width;
            DrawGradient(context.Graphics, r, _nonActiveFirstColor, _nonActiveSecondColor);

            SizeF textSize = context.Graphics.MeasureString(_text, context.Font);

            context.Graphics.DrawString(_text, _font, new SolidBrush(_foreColor),
                                        context.Bounds.X + context.Bounds.Width / 2 - textSize.Width / 2,
                                        context.Bounds.Y + context.Bounds.Height / 2 - textSize.Height / 2);
        }

        private void Update(TreeNodeAdv node)
        {
            object o = GetValue(node);

            if (o is ProgressData)
            {
                ProgressData data = (ProgressData) o;
                _value = data.Position;
                _text = data.Text;
            }
            else
            {
                if (o is int)
                    _value = (int) o;
            }
        }

        private static void DrawGradient(Graphics g, Rectangle rect, Color firstColor, Color secondColor)
        {
            Rectangle r = new Rectangle(rect.X, rect.Y, rect.Width, rect.Height / 2);
            if (rect.Width == 0)
                return;

            g.FillRectangle(new LinearGradientBrush(r, firstColor, secondColor, 90.1f), r);
            r.Y = rect.Y + rect.Height - r.Height;
            g.FillRectangle(new LinearGradientBrush(r, secondColor, firstColor, 90.1f), r);
        }
    }
}